<template>
  <div class="chart-container">
    <div class="chart-header">
      <div></div>
      <div class="pagination-dots">
        <span v-for="(_, idx) in totalPages" :key="idx" class="dot" :class="{ active: idx === currentPage }"></span>
      </div>
    </div>
    <!-- 使用 transition-group 处理列表动画 -->
    <transition-group name="list-slide" tag="div" class="ranking-list">
      <div
        v-for="(item, index) in currentPageProducts"
        :key="item.content_title"
        class="ranking-item"
        :style="{ '--animation-delay': `${index * 0.2}s` }"
      >
        <div class="rank-number" :class="`rank-${getRealRank(index)}`">
          {{ getRealRank(index) }}
        </div>
        <div class="product-info">
          <a-tooltip placement="top" color="rgba(73, 146, 255, 0.8)">
            <template #title>
              <div>标题：{{ item.content_title }}</div>
              <span>内容：{{ item.content_desc }}</span>
            </template>
            <div class="product-meta">
              <span class="product-sales">{{ item.content_title }}{{ item.content_title }}</span>
              <span v-if="item.sentiment_polarity === 'positive'" class="positive-text">积极</span>
              <span v-else-if="item.sentiment_polarity === 'neutral'" class="neutral-text">中性</span>
              <span v-else class="negative-text">消极</span>
            </div>
          </a-tooltip>
          <div class="product-growth">
            <span class="high-growth" style="margin-right: 12px">{{ item.platform_name }}</span>
            <span style="float: right" class="revenue-text">{{ item.event_time }}</span>
          </div>
        </div>
      </div>
    </transition-group>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref, computed, onUnmounted } from 'vue';
const props = defineProps({
  dataSource: {
    type: Array,
    required: false
  }
});

const hotProducts = ref([
  {
    content_title: '',
    platform_name: '',
    event_time: '',
    content_desc: '',
    sentiment_polarity: ''
  }
]);

// 分页相关数据
const currentPage = ref(0);
const itemsPerPage = 3;
const pageChangeInterval = 6000; // 6秒切换一页
let pageTimer: any = null;

// 计算属性
const totalPages = computed(() => {
  return Math.ceil(hotProducts.value.length / itemsPerPage);
});

const currentPageProducts = computed(() => {
  const start = currentPage.value * itemsPerPage;
  const end = start + itemsPerPage;
  return hotProducts.value.slice(start, end);
});

const getRealRank = (index: number) => {
  return currentPage.value * itemsPerPage + index + 1;
};

// 分页切换函数
const startPagination = () => {
  pageTimer = setInterval(() => {
    currentPage.value = (currentPage.value + 1) % totalPages.value;
  }, pageChangeInterval);
};

onMounted(() => {
  hotProducts.value = [];
  props.dataSource?.forEach((item: any) => {
    hotProducts.value.push({
      content_title: item.content_title,
      platform_name: item.platform_name,
      event_time: item.event_time,
      content_desc: item.content_desc,
      sentiment_polarity: item.sentiment_polarity
    });
  });
  startPagination();
});

onUnmounted(() => {
  if (pageTimer) clearInterval(pageTimer);
});
</script>

<style lang="scss" scoped>
/* 图表区域 */
.charts-section {
  position: relative;
  z-index: 10;
  margin-bottom: 30px;
}

.charts-grid {
  display: grid;
  grid-template-columns: repeat(12, 1fr);
  gap: 20px;
  min-height: 600px;
}

.chart-item {
  background: rgba(15, 52, 96, 0.3);
  border-radius: 12px;
  border: 1px solid rgba(0, 240, 255, 0.2);
  backdrop-filter: blur(10px);
  overflow: hidden;
  min-height: 400px;
}

.chart-large {
  grid-column: span 5;
}

.chart-medium {
  grid-column: span 4;
}

.chart-small {
  grid-column: span 3;
}

.chart-extra-small {
  grid-column: span 3;
}

.chart-container {
  height: 100%;
  display: flex;
  flex-direction: column;
  padding: 12px 20px 0;
}

.chart-header {
  position: relative;
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}

.chart-title {
  color: #00f0ff;
  font-size: 18px;
  font-weight: 600;
  margin: 0;
}

.chart-indicators {
  display: flex;
  gap: 8px;
}

.indicator {
  padding: 4px 12px;
  background: rgba(0, 240, 255, 0.1);
  border: 1px solid rgba(0, 240, 255, 0.3);
  border-radius: 6px;
  color: #7ee9ff;
  cursor: pointer;
  transition: all 0.3s ease;
  font-size: 12px;
}

.indicator.active,
.indicator:hover {
  background: rgba(0, 240, 255, 0.3);
  color: #00f0ff;
}

.chart-content {
  flex: 1;
  min-height: 300px;
  height: 100%;
}

/* 分页指示器样式 */
.pagination-dots {
  float: right;
  display: flex;
  gap: 6px;
  align-items: center;
}

.dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: rgba(0, 240, 255, 0.3);
  transition: all 0.3s ease;
  cursor: pointer;
}

.dot.active {
  background: #00f0ff;
  box-shadow: 0 0 8px rgba(0, 240, 255, 0.6);
  transform: scale(1.2);
}

/* 列表过渡动画 */
.list-slide-enter-active {
  animation: slideInRight 0.6s ease-out var(--animation-delay) both;
}

.list-slide-leave-active {
  position: absolute;
  animation: slideOutLeft 0.4s ease-in both;
}

.list-slide-move {
  transition: transform 0.6s ease;
}

.ranking-list {
  position: relative;
  height: 100%;
  padding-right: 4px;
}

.ranking-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px;
  background: rgba(0, 240, 255, 0.05);
  border-radius: 8px;
  border: 1px solid rgba(0, 240, 255, 0.1);
  transition: all 0.2s ease;
  position: relative;
  overflow: hidden;
  max-height: 65px;
  margin-bottom: 8px;
  /* 移除原有的动画定义 */
}

/* 从右到左滑入动画 */
@keyframes slideInRight {
  from {
    opacity: 0;
    transform: translateX(100%);
  }

  to {
    opacity: 1;
    transform: translateX(0);
  }
}

/* 从左到左滑出动画 */
@keyframes slideOutLeft {
  from {
    opacity: 1;
    transform: translateX(0);
  }

  to {
    opacity: 0;
    transform: translateX(-100%);
  }
}

.ranking-item::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(45deg, transparent, rgba(0, 240, 255, 0.03), transparent);
  opacity: 0;
  transition: opacity 0.3s ease;
  z-index: -1;
}

.ranking-item:hover {
  background: rgba(0, 240, 255, 0.1);
  transform: translateX(2px);
  border-color: rgba(0, 240, 255, 0.3);
}

.ranking-item:hover::before {
  opacity: 1;
}

.rank-number {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
  font-size: 12px;
  color: #fff;
  flex-shrink: 0;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
}

.rank-1 {
  background: linear-gradient(135deg, #ffd700, #ffed4e);
  box-shadow: 0 2px 12px rgba(255, 215, 0, 0.4);
}

.rank-2 {
  background: linear-gradient(135deg, #c0c0c0, #ddd);
  box-shadow: 0 2px 12px rgba(192, 192, 192, 0.4);
}

.rank-3 {
  background: linear-gradient(135deg, #cd7f32, #d4af37);
  box-shadow: 0 2px 12px rgba(205, 127, 50, 0.4);
}

.rank-number:not(.rank-1):not(.rank-2):not(.rank-3) {
  background: linear-gradient(135deg, rgba(0, 240, 255, 0.3), rgba(0, 240, 255, 0.5));
}

.product-info {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.product-name {
  color: #fff;
  font-size: 13px;
  font-weight: 600;
  margin-bottom: 3px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  line-height: 1.3;
}

.product-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 6px;
}

.product-category {
  color: rgba(0, 240, 255, 0.8);
  font-size: 11px;
  background: rgba(0, 240, 255, 0.1);
  padding: 2px 6px;
  border-radius: 10px;
  border: 1px solid rgba(0, 240, 255, 0.2);
  white-space: nowrap;
}

.product-sales {
  color: rgba(255, 255, 255, 0.7);
  font-size: 14px;
  font-weight: 500;
  white-space: nowrap;
  text-overflow: ellipsis;
  display: inline-block;
  overflow: hidden;
}

.product-growth {
  font-size: 10px;
  font-weight: 600;
  line-height: 1;
}

.high-growth {
  color: #00ff9d;
}

.normal-growth {
  color: #ffa502;
}

.revenue-text {
  color: #00ff9d;
  font-size: 11px;
  white-space: nowrap;
}

.positive-text {
  min-width: 40px;
  padding: 2px 4px;
  margin-bottom: 6px;
  text-align: center;
  font-size: 12px;
  border-radius: 4px;
  border: 1px solid #ffa502;
  color: #ffa502;
}

.neutral-text {
  min-width: 40px;
  padding: 2px 4px;
  margin-bottom: 6px;
  text-align: center;
  font-size: 12px;
  border-radius: 4px;
  border: 1px solid #82f8cb;
  color: #82f8cb;
}

.negative-text {
  min-width: 40px;
  padding: 2px 4px;
  margin-bottom: 6px;
  text-align: center;
  font-size: 12px;
  border-radius: 4px;
  border: 1px solid #0fa5b6;
  color: #0fa5b6;
}

.sales-bar {
  width: 60px;
  height: 4px;
  background: rgba(0, 240, 255, 0.2);
  border-radius: 2px;
  overflow: hidden;
  box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.2);
}

.bar-fill {
  height: 100%;
  background: linear-gradient(90deg, #00f0ff, #00ff9d);
  border-radius: 2px;
  transition: width 1s ease;
  position: relative;
}

.bar-fill::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.3), transparent);
  animation: shimmer 2s infinite;
}

@keyframes shimmer {
  0% {
    transform: translateX(-100%);
  }

  100% {
    transform: translateX(100%);
  }
}

/* 排行榜滚动条美化 */
.ranking-list::-webkit-scrollbar {
  width: 3px;
}

.ranking-list::-webkit-scrollbar-track {
  background: rgba(0, 240, 255, 0.05);
  border-radius: 2px;
}

.ranking-list::-webkit-scrollbar-thumb {
  background: linear-gradient(180deg, rgba(0, 240, 255, 0.3), rgba(0, 255, 157, 0.3));
  border-radius: 2px;
}

.ranking-list::-webkit-scrollbar-thumb:hover {
  background: linear-gradient(180deg, rgba(0, 240, 255, 0.5), rgba(0, 255, 157, 0.5));
}
</style>
